package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.LinkDao;
import cz.khfotbal.domain.Link;

@Repository
@Transactional
public class LinkDaoImpl implements LinkDao{

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;
	
	public List<Link> findAllLinks() {
		session = sessionFactory.getCurrentSession();
		List<Link> list = session.createQuery("from Link").list();
		return list;
	}

	public void saveLink(Link link) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(link);
	}

	public void updateLink(Link link) {
		session = sessionFactory.getCurrentSession();
		session.merge(link);
	}

	public Link findLinkById(int id) {
		session = sessionFactory.getCurrentSession();
		Link link = (Link) session.createQuery("from Link where id = "+id).uniqueResult();
		return link;
	}

	public void deleteLink(Link link) {
		session = sessionFactory.getCurrentSession();
		session.delete(link);
	}

}
